package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.entity.SelectGoodsVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjInstructionBook;
import com.zb.extend.KnowledgeTypeInstructionBookExtend;
import com.zb.mapper.ZjInstructionBookMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjInstructionBookService;
import com.zb.service.ZjInstructionBooklContentService;
import com.zb.service.ZjKnowledgeTypeInstructionBookService;
import com.zb.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品说明书表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Service
public class ZjInstructionBookServiceImpl extends ServiceImpl<ZjInstructionBookMapper, ZjInstructionBook> implements ZjInstructionBookService {
    @Resource
    private ZjInstructionBookMapper instructionBookMapper;
    @Resource
    private ZjInstructionBooklContentService booklContentService;
    @Resource
    private ZjKnowledgeTypeInstructionBookService knowledgeTypeInstructionBookService;

    // 新增或修改说明书
    @Override
    @Transactional
    public void save(Long custAccId, InstructionBookVo instructionBookVo) {
        Assert.isTrue(null == instructionBookMapper.existByName(custAccId, instructionBookVo.getId(), instructionBookVo.getName()), "说明书已重复", instructionBookVo.getName());

        ZjInstructionBook book = new ZjInstructionBook();
        BeanUtils.copyProperties(instructionBookVo, book);
        book.setDisabled(instructionBookVo.getStatus());
        if (null != book.getId()) {
            this.updateById(book);
        } else {
            book.setCustAccId(custAccId);
            book.setClickNum(0);
            this.save(book);
        }
        booklContentService.save(custAccId, book.getId(), instructionBookVo.getContent());
        knowledgeTypeInstructionBookService.save(custAccId, book.getId(), instructionBookVo.getKnowledgeTypeVos(), instructionBookVo.getDelRelationIds());
    }

    // 获取说明书列表
    @Override
    public Map<String, Object> getList(Long custAccId, InstructionBookSelectVo bookSelectVo) {
        QueryWrapper<ZjInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(bookSelectVo.getName())) {
            wrapper.like("name", bookSelectVo.getName());
        }
        PageUtil.setPageLimit(bookSelectVo);
        Page<ZjInstructionBook> page = this.page(new Page<>(bookSelectVo.getPage(), bookSelectVo.getLimit()), wrapper);

        List<KnowledgeTypeInstructionBookExtend> bookExtendList = knowledgeTypeInstructionBookService.getByInstructionBookIds(page.getRecords().stream().map(ZjInstructionBook::getId).toList());

        List<InstructionBookListVo> voList = page.getRecords().stream().map(t -> {
            InstructionBookListVo vo = InstructionBookListVo.beanToVo(t);
            vo.setKnowledgeType(bookExtendList.stream().filter(f -> Objects.equals(f.getInstructionBookId(), t.getId())).map(KnowledgeTypeInstructionBookExtend::getName).collect(Collectors.joining("、")));
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 获取说明书信息
    @Override
    public InstructionBookVo getInfo(Long id) {
        ZjInstructionBook book = this.getById(id);
        Assert.isTrue(null != book, "["+ id +"]的数据不存在！", null);

        InstructionBookVo vo = new InstructionBookVo();
        BeanUtils.copyProperties(book, vo);
        vo.setStatus(book.getDisabled());
        vo.setContent(booklContentService.getContentById(id));
        vo.setKnowledgeTypeVos(knowledgeTypeInstructionBookService.getByInstructionBookId(id).stream().map(type -> {
            return new KnowledgeTypeRelationVo(type.getId(), type.getKnowledgeTypeId());
        }).toList());
        return vo;
    }

    // 批量启用或禁用说明书
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean disable) {
        instructionBookMapper.updateBatchDisableByIds(idList, disable);
    }

    // 根据说明书ID集合查询名称
    @Override
    public List<String> getNameByIds(List<Long> idList) {
        if (null == idList || idList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        wrapper.in("id", idList);
        return this.list(wrapper).stream().map(ZjInstructionBook::getName).toList();
    }

}
